﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
using TrinityAPI.Handler;

namespace TrinityAPI
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
            GlobalConfiguration.Configuration.MessageHandlers.Add(new APIRequestHandler());
        }

        public void Application_BeginRequest()
        {
            //AJAX跨域请求时，会先用OPTIONS方法预处理。直接返回204，保证该请求不会执行任何逻辑
            if (Request.HttpMethod?.ToUpper() == "OPTIONS")
            {
                Response.StatusCode = 204;
                Response.End();
            }
        }
    }
}
